﻿
using StrokeCenterSystem.Bll;
using StrokeCenterSystem.Code.Bll;
using StrokeCenterSystem.Core;
using StrokeCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StrokeCenterSystem.Areas.StrokeCenter.Models;

namespace StrokeCenterSystem.Areas.StrokeCenter.Controllers
{
    public class PathwayListController : BaseController
    {
        //
        // GET: /StrokeCenter/PathwayList/

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 加载诊疗路径列表
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            //Name=s&page=1&pagesize=30
            string pathName = WebUtils.GetString("Name");
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pagesize", 30);
            OperateResult resultInfo = PathwayManager.GetDiagnosticPath(pageIndex, pageSize, pathName);
            string NoDate = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1)
            {
                if (resultInfo.Body.Equals("未检索到数据！")) { return Content(NoDate); }
                //必须参数 Total Rows
                //var result = new { Total = jsonObj.PatientNum.Value, Rows = jsonObj.PatientList, InHospitalNum = jsonObj.PatientNum.Value   };
                var jsonObj = JsonConvert.DeserializeObject<List<dynamic>>(resultInfo.Body.ToString());
                var total = jsonObj.Count;
                var result = new { Total = total, Rows = jsonObj };
                return JsonDate(result);
            }
            return Content(NoDate);
        }

        /// <summary>
        /// 添加/编辑路径
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            int id = WebUtils.GetInt("id");
            if (id.Equals(0))
            {
                //新增 赋空
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("Id");
                dt.Columns.Add("Sort");
                dt.Columns.Add("AddTime");
                dt.Columns.Add("ModifyTime");
                DataRow dr = dt.NewRow();
                dr["Name"] = "";
                dr["Sort"] = 0;
                dr["Id"] = id;
                dr["AddTime"] = "";
                dr["ModifyTime"] = "";
                dt.Rows.Add(dr);
                ViewBag.Data = dt;
                return View();
            }
            else
            {
                //编辑 取数据赋值
                OperateResult result = PathwayManager.GetDiagnosticPathById(id);
                if (result.Result == 1)
                {
                    DiagnosticPath diagnosticPath = JsonConvert.DeserializeObject<DiagnosticPath>(result.Body.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Name");
                    dt.Columns.Add("Id");
                    dt.Columns.Add("Sort");
                    dt.Columns.Add("AddTime");
                    dt.Columns.Add("ModifyTime");
                    DataRow dr = dt.NewRow();
                    dr["Name"] = diagnosticPath.Name;
                    dr["Sort"] = diagnosticPath.Sort;
                    dr["Id"] = id;
                    dr["AddTime"] = diagnosticPath.UpdateTime.ToString();
                    dr["ModifyTime"] = diagnosticPath.CreateTime.ToString();
                    dt.Rows.Add(dr);
                    ViewBag.Data = dt;
                    return View();
                }
                return Content(result.Body.ToString());
            }
        }

        /// <summary>
        /// 保存新增或者编辑信息
        /// </summary>
        /// <returns></returns>
        public ActionResult EditSubmit()
        {
            string pathwayName = WebUtils.GetString("pathwayName");
            string id = WebUtils.GetString("Id");
            string pathJson = "{\"ID\":\"" + id + "\",\"Name\":\"" + pathwayName + "\",\"CreateUserId\":\"" + LoginManager.GetLoginUser().Id + "\"}";
            OperateResult result = PathwayManager.SavePathName(pathJson);
            return Content(result.Body.ToString());
        }

        public ActionResult GetPathNameList()
        {
            OperateResult resultInfo = PathwayManager.GetPathNameList();
            return JsonDate(resultInfo);
        }
    }
}
